#include<iostream>
using namespace std;

#include<string>
#include<vector>
#include<list>

void test_string1()
{
	// 1.size | length
	string s1("hello string");
	// 两者一样的 但我们通常用size,size是通用的使用于链表二叉树等等
	cout << s1.size() << endl;
	cout << s1.length() << endl;
	cout << "————————————————————" << endl;
	// 2.max_size
	string s2;
	cout << s1.max_size() << endl;
	cout << s2.max_size() << endl;
	cout << "————————————————————" << endl;
	// 3.capacity
	cout << s1.capacity() << endl;
	cout << "————————————————————" << endl;
	// 4.clear
	string s3("hello clear");
	cout << "清空前：" << s3 << endl;
	cout << "容量：" << s3.capacity() << endl;
	s3.clear();
	cout << "清空后：" << s3 << endl;
	cout << "容量：" << s3.capacity() << endl;
	cout << "———————————————————————" << endl;
	// 5.reserve和resize
	//     保留    逆置
	// 查看扩容机制
	string s;
	s.reserve(100);

	size_t sz = s.capacity();
	cout << "capacity changed: " << sz << '\n';
	cout << "making s grow:\n";
	for (int i = 0; i < 100; ++i)
	{
		s.push_back('C');
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}
}
void test_string2()
{
	string s1("hello string");
	s1.reserve(100);

	string s2("hello string");
	s2.resize(100);

	string s3;
	s3.resize(100, 'x'); // 给值，指定初始化字符

	string s4("hello world");
	s4.resize(5);
}
void test_string3()
{
	string s1("hello worldxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
	cout << s1.size() << endl;
	cout << s1.length() << endl;

	cout << s1.max_size() << endl;
	cout << s1.capacity() << endl;

	// 查看扩容机制
	string s;
	s.reserve(100);// 提前开好空间，这时候就没有扩容了

	size_t sz = s.capacity();
	cout << "capacity changed: " << sz << '\n';
	cout << "making s grow:\n";
	for (int i = 0; i < 100; ++i)
	{
		s.push_back('c');
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}

//	cout << s1 << endl;
//	cout << s1.capacity() << endl;
//	cout << s1.size() << endl;
//
//
//	s1.clear();
//	cout << s1 << endl;
//	cout << s1.capacity() << endl;
//	cout << s1.size() << endl;
//
//	// 缩容
//	s1.shrink_to_fit();
//	cout << s1.capacity() << endl;
//	cout << s1.size() << endl;
}
void test_string5()
{
	string s1("hello worldxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
	cout << s1.size() << endl;
	cout << s1.capacity() << endl << endl;

	s1.reserve(10);
	cout << s1.size() << endl;
	cout << s1.capacity() << endl << endl;
	cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;

	string s2("hello worldXXXXX");
	cout << s2.size() << endl;
	cout << s2.capacity() << endl << endl;

	s2.resize(10);
	cout << s2.size() << endl;
	cout << s2.capacity() << endl << endl;

	s2.resize(20);
	cout << s2.size() << endl;
	cout << s2.capacity() << endl << endl;

	s2.resize(40);
	cout << s2.size() << endl;
	cout << s2.capacity() << endl << endl;

	s2.resize(100, 'x');
	cout << s2.size() << endl;
	cout << s2.capacity() << endl << endl;
}

void test_string6()
{
	string s1("xhello world");
	cout << s1[5] << endl;
	cout << s1.at(5) << endl;

	// 抛异常
	try
	{
		//s1[15];
		//s1.at(15);
	}
	catch (const exception& e)
	{
		cout << e.what() << endl;
	}
}
void test_string7()
{
	string s1("xhello world");
	// 末尾插入一个字符
	s1.push_back('!');
	cout << s1 << endl;

	// 末尾插入字符串
	s1.append("hello bit");
	cout << s1 << endl;
	// 插入一个字符
	s1.append(10,'x');
	cout << s1 << endl;

}
int main()
{
	//test_string1();
	//test_string2();
	//test_string3();
	//test_string5();
	//test_string6();
	test_string7();

	return 0;
}